package com.emspost.utils;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class AESKeyGenerator {

    public static String generateAESKey(int keySize) throws NoSuchAlgorithmException {
        // 1. 指定算法和密钥长度
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(keySize, new SecureRandom()); // 使用安全随机数
        
        // 2. 生成密钥
        SecretKey secretKey = keyGen.generateKey();
        
        // 3. 转换为 Base64 字符串（便于存储）
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    public static void main(String[] args) throws Exception {
        String aes128Key = generateAESKey(128);
        String aes256Key = generateAESKey(256);
        
        System.out.println("AES-128 Key (Base64): " + aes128Key);
        System.out.println("AES-256 Key (Base64): " + aes256Key);
    }
}